from django.test import TestCase
from django.urls import reverse
from django.contrib.auth.models import User
from .models import Profile

class ProfileDetailViewTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        # 创建测试用户和个人资料
        cls.user1 = User.objects.create_user(username='testuser1', password='12345')
        Profile.objects.create(user=cls.user1, nickname='Test Nickname 1', full_name='Test Full Name 1', education='Test Education 1', interested_position='Test Position 1')
        cls.user2 = User.objects.create_user(username='testuser2', password='12345')
        Profile.objects.create(user=cls.user2, nickname='Test Nickname 2', full_name='Test Full Name 2', education='Test Education 2', interested_position='Test Position 2')

    def test_profile_detail_view_user1(self):
        self.client.login(username='testuser1', password='12345')
        response = self.client.get(reverse('profile_detail', kwargs={'username': 'testuser1'}))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {
            'username': 'testuser1',
            'nickname': 'Test Nickname 1',
            'full_name': 'Test Full Name 1',
            'education': 'Test Education 1',
            'interested_position': 'Test Position 1',
            'blog_or_repo_link': '',
            'work_experience': None,
            'job_title': None,
            'resume': None,
            'can_add_extra_info': False,
        })

    def test_profile_detail_view_user2(self):
        self.client.login(username='testuser2', password='12345')
        response = self.client.get(reverse('profile_detail', kwargs={'username': 'testuser2'}))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {
            'username': 'testuser2',
            'nickname': 'Test Nickname 2',
            'full_name': 'Test Full Name 2',
            'education': 'Test Education 2',
            'interested_position': 'Test Position 2',
            'blog_or_repo_link': '',
            'work_experience': '',
            'job_title': '',
            'resume': None,
            'can_add_extra_info': False,
        })

class ProfileUpdateViewTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        # 创建测试用户和个人资料
        cls.user1 = User.objects.create_user(username='testuser1', password='12345')
        Profile.objects.create(user=cls.user1, nickname='Test Nickname 1', full_name='Test Full Name 1', education='Test Education 1', interested_position='Test Position 1')

    def setUp(self):
        # 每个测试前登录用户
        self.client.login(username='testuser1', password='12345')

    def test_profile_update_view_invalid_empty_fields(self):
        # 测试无效的表单提交（字段为空），验证返回的错误信息是否正确
        response = self.client.post(reverse('profile_update', kwargs={'username': 'testuser1'}), {
            'nickname': '',
            'full_name': '',
            'education': '',
            'interested_position': ''
        })
        self.assertEqual(response.status_code, 400)
        self.assertIn('nickname', response.json()['errors'])
        self.assertIn('full_name', response.json()['errors'])
        self.assertIn('education', response.json()['errors'])
        self.assertIn('interested_position', response.json()['errors'])

    def test_profile_update_view_invalid_url(self):
        # 测试无效的 URL 提交，验证返回的错误信息是否正确
        response = self.client.post(reverse('profile_update', kwargs={'username': 'testuser1'}), {
            'nickname': 'Valid Nickname',
            'full_name': 'Valid Full Name',
            'education': 'Valid Education',
            'interested_position': 'Valid Position',
            'blog_or_repo_link': 'invalid-url'
        })
        self.assertEqual(response.status_code, 400)
        self.assertIn('blog_or_repo_link', response.json()['errors'])

    def test_profile_update_view_partial_update(self):
        # 测试部分更新资料，验证数据更新是否正确
        response = self.client.post(reverse('profile_update', kwargs={'username': 'testuser1'}), {
            'nickname': 'Updated Nickname',
            'education': 'Updated Education'
        })
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json()['success'], True)
        self.assertEqual(response.json()['data']['nickname'], 'Updated Nickname')
        self.assertEqual(response.json()['data']['education'], 'Updated Education')
